package eg.com.itiga.business.company;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import eg.com.itiga.business.base.BaseServiceImpl;
import eg.com.itiga.business.exception.BusinessException;
import eg.com.itiga.domain.Company;
import eg.com.itiga.domain.ItianCompany;
import eg.com.itiga.domain.RegCompany;
import eg.com.itiga.repository.company.CompanyDao;
import eg.com.itiga.repository.exception.DataException;
import eg.com.itiga.repository.factory.RepositoryFactory;

public class CompanyServiceImpl extends BaseServiceImpl implements CompanyService {
	
	public RegCompany retriveCompanyData(int id) throws BusinessException {
        RegCompany c = null;
        try
        {
            CompanyDao companyDaoObject= RepositoryFactory.createCompanyDao();
            System.out.println("BRFOOOOOORE BEGIN TRANSACTIIION");
            companyDaoObject.beginTransaction();
            System.out.println("BRFOOOOOORE BEGIN TRANSACTIIION");
            c= companyDaoObject.retriveCompanyData(id);
            System.out.println("BRFOOOOOORE BEGIN TRANSACTIIION");
            companyDaoObject.commit();
       
        }
        catch(DataException ds)
        {
            System.out.println("you logined and try to view the itian profile");
        }
        return c;
    }

	public Hashtable<String, Integer> getItiansInACompany(int companyID) throws BusinessException 
	{
		Hashtable<String, Integer> list = new Hashtable<String, Integer>();
		
		CompanyDao companyDao = null;
		try 
		{
			companyDao = RepositoryFactory.createCompanyDao();
			companyDao.beginTransaction();
			
			Company company = (Company)companyDao.find(Company.class, companyID);
			
			Set itiansCompanies =  company.getItianCompanies();
			
			for (Iterator iterator = itiansCompanies.iterator(); iterator
					.hasNext();) {
				ItianCompany itianCompany = (ItianCompany) iterator.next();
				
				String deptName = itianCompany.getItian().getDepartment().getDName();
				Integer counter = 0;
				
				if(list.containsKey(deptName))
				{
					counter = list.get(deptName);
					counter++;
					
					list.remove(deptName);
				}
				
				list.put(deptName, counter);
			}
			
			companyDao.commit();
		} 
		catch (DataException e) {
			companyDao.rollback();
			e.printStackTrace();
		}
		return list ;
	}
	
	public List<Company> getCompanyByStatus(boolean status)
	throws BusinessException {
		// TODO Auto-generated method stub
		List<Company> res = null;
		try
		{
			CompanyDao companyDao = RepositoryFactory.createCompanyDao();
			res = companyDao.getCompanyByStatus(status);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		return res;
	}
	
	public void update(Object object) throws BusinessException {
		// TODO Auto-generated method stub
		//CompanyDao c=null;
		//BaseDao b=null;
		RegCompany rg=(RegCompany)object;
		RegCompany regCom=null;
		CompanyDao companyDao= null;
		System.out.println("YES inside the service Impl------"+rg.getCName()+"---:)");
		try
		{
			
			companyDao = (CompanyDao) RepositoryFactory.createCompanyDao();
			
			System.out.println("The id of the user that retrieved = = "+rg.getUser().getUserId());
		companyDao.beginTransaction();
		regCom =companyDao.retriveCompanyData(rg.getUser().getUserId());
		rg.setCId(regCom.getCId());
		System.out.println("The id of the user that retrieved = = "+rg.getUser().getUserId());
	
		
		System.out.println("The id of the new company that retrieved = = "+regCom.getCId());
		System.out.println("The WebSite of the new company that retrieved = = "+regCom.getRegWesite());
		System.out.println("The WebSite of the new company that changed = = "+rg.getRegWesite());
		companyDao.updateCompany(rg);
		//companyDao.evictObject(rg);
		System.out.println("The WebSite of the new company that changed AFTER EVICETOBJECT "+rg.getRegWesite());
		companyDao.commit();
		
		
		}
		catch(DataException dataExceptionCompany)
		{
			dataExceptionCompany.printStackTrace();
			
		}
	}
}
